package com.roocon.thread.ExceptionHandler;

public class ThreadGroupDemo {

    public static void main(String[] args) {
        // 1.创建线程组
        ThreadGroup threadGroup = new ThreadGroup("group") {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                // 4.处理捕获的线程异常
                System.out.println(t.getName() + " has thrown an exception: " + e.getMessage());
            }
        };

        // 2.创建Thread
        Thread thread = new Thread(threadGroup, new Runnable() {
            @Override
            public void run() {
                System.out.println("This will throw an ArithmeticException because of division by zero22.");
//                try {
//                    Thread.sleep(800);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
                System.out.println(1 / 0);
            }
        }, "my_thread");

        // 3.启动线程
        thread.start();

        // 2.创建Thread
        Thread thread2 = new Thread(threadGroup, new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                throw new RuntimeException("error");
            }
        }, "my_thread1");

        // 3.启动线程
        thread2.start();

    }
}
